'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System.Linq
Imports System.Collections.ObjectModel

Public Class SearchWindowViewModel
    Inherits System.Windows.Threading.DispatcherObject
    Implements System.ComponentModel.INotifyPropertyChanged

    Private _SearchResult As Collection(Of ContactViewModel)

    Public Sub New()
        Dim sv As IcqInterface.Interfaces.ISearchService

        sv = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.ISearchService)()

        AddHandler sv.SearchResult, AddressOf OnSearchResult
    End Sub

    Private Sub OnSearchResult(ByVal sender As Object, ByVal e As IcqInterface.Interfaces.SearchResultEventArgs)
        Try
            _SearchResult = New Collection(Of ContactViewModel)((From x In e.Contacts Select ContactViewModelCache.GetViewModel(x)).ToList)

            OnPropertyChanged("SearchResult")
        Catch ex As Exception
            Core.Kernel.Exceptions.PublishException(ex)
        End Try
    End Sub

    Public ReadOnly Property SearchResult() As Collection(Of ContactViewModel)
        Get
            Return _SearchResult
        End Get
    End Property

    Public Sub Search(ByVal uinQuery As String)
        Dim sv As IcqInterface.Interfaces.ISearchService

        sv = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.ISearchService)()

        sv.SearchByUin(uinQuery)
    End Sub

    Public Sub BuildContactContextMenu(ByVal menu As ContextMenu, ByVal contact As ContactViewModel)
        Dim sv As IContactContextMenuService

        sv = ApplicationService.Current.Context.GetService(Of IContactContextMenuService)()
        menu.Items.Clear()

        For Each x As MenuItem In sv.GetMenuItems(contact)
            menu.Items.Add(x)
        Next
    End Sub

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Protected Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(propertyName))
    End Sub
End Class
